#include <rclcpp/rclcpp.hpp>
#include <std_msgs/msg/string.hpp>

using namespace std::chrono_literals;

using String = std_msgs::msg::String;

class SimplePublisher: public rclcpp::Node
{
public:
  SimplePublisher(const std::string &node_name): Node(node_name)
  {
    counter_ = 0;
    frequency = 1;

    pub_ = this->create_publisher<String>(
      "chater", 10       
    );
    timer_ = this->create_wall_timer((1000ms/frequency),
      std::bind(&SimplePublisher::timer_callback, this));
    
    RCLCPP_INFO(this->get_logger(), "Publishing as 1 Hz");
  }

private :
  void timer_callback()
  {
    auto msg = String();
    msg.data = "Hello ROS2 - counter: " + std::to_string(counter_);

    // RCLCPP_INFO(this->get_logger(), "Publishing counter: %d", counter_);

    pub_->publish(msg);
    counter_++;
  }

  rclcpp::Publisher<String>::SharedPtr pub_;
  rclcpp::TimerBase::SharedPtr timer_;
  unsigned int counter_;
  unsigned int frequency;
};

int main(int argc, char *argv[])
{
  rclcpp::init(argc, argv);
  auto node = std::make_shared<SimplePublisher>("simple_publisher");
  rclcpp::spin(node);
  rclcpp::shutdown();

  return 0;
}